using System;
using System.Linq;
using System.Collections.Generic;
using Managistics.Model;
using NHibernate;
using NHibernate.Action;
using NHibernate.Criterion;
using Managistics.Utility.Common;

namespace Managistics.Core.Repositories.Impl
{
    public class AdjustInStockRepository : RepositoryBase<AdjustInStock, int>, IAdjustInStockRepository
    {

        /// <summary>
        /// Check to known that the stock has been adjusted in mont/year
        /// </summary>
        /// <param name="month">The month option to check</param>
        /// <param name="year">The year option to check</param>
        /// <param name="parityCode">The parity code or empty to check all goods</param>
        /// <returns></returns>
        public bool HasAdjustedInMonth(int month, int year, string parityCode = "")
        {
            return (this.GetGoodsAdjustedInMonth(month, year, parityCode).Count() > 0);
        }

        /// <summary>
        /// Get all adjusted goods in month
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public IList<AdjustInStock> GetGoodsAdjustedInMonth(int month, int year, string parityCode)
        {
            var query =
                this.Session.QueryOver<AdjustInStock>()
                .Where(Restrictions.Eq(
                    Projections.SqlFunction("year",
                    NHibernate.NHibernateUtil.Int32,
                    Projections.Property<AdjustInStock>(o => o.AdjustedDate)), year))
                .And(Restrictions.Eq(
                    Projections.SqlFunction("month",
                    NHibernate.NHibernateUtil.Int32,
                    Projections.Property<AdjustInStock>(o => o.AdjustedDate)), month));

            // check parity good has adjust in month/year
            if (StringExtensions.IsNotNullOrEmpty(parityCode))
            {
                query.Where(ad => ad.Good.Code == parityCode);
            }

            return query.List<AdjustInStock>();
        }

        /// <summary>
        /// Remove all adjust sheets in month/year
        /// </summary>
        /// <param name="smonth"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public int RemoveAllAdjustInMonth(int month, int year)
        {
            var result =
                this.Session.CreateSQLQuery("DELETE FROM :table WHERE YEAR(AdjustedDate) = :year AND MONTH(AdjustedDate) = :month; SELECT @@ROWCOUNT count;")
                .AddScalar("count", NHibernateUtil.Int32)
                .SetParameter("table", Constants._tblAdjustInStock)
                .SetParameter("year", year)
                .SetParameter("month", month)
                .UniqueResult();

            return Convert.ToInt32(result);
        }

        public void SaveOrUpdateGood(AdjustInStock adjust)
        {
            this.Session.SaveOrUpdate(adjust);
            this.Session.Flush();
        }
    }
}